// 文件大小转换
export function formatBytes (a, b) {
  if (a === 0) return '0 B'
  const c = 1024
  const d = b || 2
  const e = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
  const f = Math.floor(Math.log(a) / Math.log(c))
  return parseFloat((a / Math.pow(c, f)).toFixed(d)) + ' ' + e[f]
}

/**
 * 数组转树形结构
 * @param list 源数组
 * @param tree 树
 * @param parentId 父ID
 */
export const listToTree = (list, tree, parentId, isMenu) => {
  list.forEach(item => {
    // 判断是否为父级菜单
    if (isMenu && item.hidden) {
      return
    }
    if (item.parentId === parentId) {
      const child = {
        ...item,
        key: item.key || item.name || item.majorName || '',
        children: []
      }
      // 迭代 list， 找到当前菜单相符合的所有子菜单
      listToTree(list, child.children, item.id, isMenu)
      // 删掉不存在 children 值的属性
      if (child.children.length <= 0) {
        delete child.children
      }
      // 加入到树中
      tree.push(child)
    }
  })
}
// 获取地址栏参数key对应值
export const getQueryString = (name) => {
  const { href } = window.location
  const params = href.split('?')
  for (let j = 0; j < params.length; j++) {
    const querys = params[j].split('&')
    for (var i = 0; i < querys.length; i++) {
      const pair = querys[i].split('=')
      if (pair[0] === name) {
        return pair[1]
      }
    }
  }
}
